//
//  KBaseViewController.m
//  Peanut
//
//  Created by Jim huang on 2018/3/12.
//  Copyright © 2018年 Jim_Peanut. All rights reserved.
//

#import "KBaseViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface KBaseViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate,UIGestureRecognizerDelegate>

@property(nonatomic,weak)MBProgressHUD* mbHUD;


@end

@implementation KBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

#pragma mark ----相机相册部分----
-(void)checkCameraAvailability:(void (^)(BOOL))block{
    BOOL available = NO;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusAuthorized) {
        available = YES;
    }else if (authStatus == AVAuthorizationStatusDenied){
        available = NO;
    }else if (authStatus == AVAuthorizationStatusRestricted){
        available = NO;
    }else if (authStatus == AVAuthorizationStatusNotDetermined){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (block) {
                block(granted);
            }
        }];
        return;
    }
    if (block) {
        block(available);
    }
}
-(void)getImageWIthChannel:(UIImagePickerControllerSourceType)sourceType{
    switch (sourceType) {
        case UIImagePickerControllerSourceTypeCamera:
        {
            [self getImageWithType:UIImagePickerControllerSourceTypeCamera];
        }
            break;
        case UIImagePickerControllerSourceTypePhotoLibrary:
        {
            [self getImageWithType:UIImagePickerControllerSourceTypePhotoLibrary];
        }
            break;
        case UIImagePickerControllerSourceTypeSavedPhotosAlbum:
        {
            [self getImageWithType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
        }
            break;
        default:
            break;
    }
}


-(void)getImageWithType:(UIImagePickerControllerSourceType)sourceType{
    if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
        if (UIImagePickerControllerSourceTypePhotoLibrary == sourceType) {
            [self showErrorText:@"请允许访问相册"];
        }else if (UIImagePickerControllerSourceTypeCamera == sourceType){
            [self showErrorText:@"请允许使用相机"];
        }
    }else{
        UIImagePickerController* imagePicker = [UIImagePickerController new];
        imagePicker.delegate = self;
        imagePicker.sourceType = sourceType;
        imagePicker.allowsEditing = self.CameraImageEditing;
        if (UIImagePickerControllerSourceTypeCamera == sourceType) {
            imagePicker.showsCameraControls = YES;
            imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        }
        imagePicker.mediaTypes = @[(NSString*)kUTTypeImage];
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage* image = nil;
    if (self.CameraImageEditing) {
        image = info[UIImagePickerControllerEditedImage];
    }else{
        image = info[UIImagePickerControllerOriginalImage];
    }
    [picker dismissViewControllerAnimated:YES completion:^{
        if ([self.imageDelegate respondsToSelector:@selector(didGetImage:Info:)]) {
            [self.imageDelegate didGetImage:image Info:info];
        }
        if ([self.imageDelegate respondsToSelector:@selector(pickingMediaWithInfo:)]) {
            [self.imageDelegate pickingMediaWithInfo:info];
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
